//
//  PaperTestScrollView.m
//  智云题库
//
//  Created by 毕武侠 on 2017/3/22.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperTestScrollView.h"

@interface PaperTestScrollView ()

@property (nonatomic, strong) YYLabel *textLabel;    // 题干
@property (nonatomic, strong) YYLabel *answerLabel;  // 答案
@property (nonatomic, strong) UIButton *answerBtn;   // 答案展示按钮
@property (nonatomic, strong) PaperTestModel *model;

@end

@implementation PaperTestScrollView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupConfigure];
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupConfigure];
    }
    return self;
}

- (void)setupConfigure {
    self.backgroundColor = [UIColor whiteColor];
    //可以达到同一界面上多个控件接受事件时的排他性,从而避免一些问题。也就是说避免在一个界面上同时点击多个button。
    self.exclusiveTouch = YES;
    // 加载题目View
    self.textLabel.frame = CGRectMake(12, 6, self.width - 24.0f, 12);
    [self addSubview:self.textLabel];
    
    // 加载解析按钮
    [self addSubview:self.answerBtn];
    
    // 加载答案View
    self.answerLabel.frame = CGRectMake(12, 0, self.width - 24.0f, 12);
    [self addSubview:self.answerLabel];
}

- (void)refreshView {
    [self setContentOffset:CGPointZero animated:NO];
    
    // text
    self.textLabel.height = self.model.textLayout.textBoundingSize.height;
    self.textLabel.textLayout = self.model.textLayout;
    
    [self showAnswerViewWithType:self.model.isShow];
}

- (void)showAnswerViewWithType:(BOOL)type {
    if (type == YES) {
        // btn
        self.answerBtn.hidden = YES;
        
        // answer
        self.answerLabel.hidden = NO;
        self.answerLabel.top = self.textLabel.bottom + 42;
        self.answerLabel.height = self.model.answerLayout.textBoundingSize.height;
        self.answerLabel.textLayout = self.model.answerLayout;
        
        self.contentSize = CGSizeMake(SCREEN_WIDTH, self.answerLabel.bottom + 32.0);
    } else {
        // btn
        self.answerBtn.hidden = NO;
        self.answerBtn.top = self.textLabel.bottom  + 40 < 140 ? 140 : self.textLabel.bottom + 40;
        
        // answer
        self.answerLabel.hidden = YES;
        self.answerLabel.top = self.textLabel.bottom + 42;
        self.answerLabel.height = self.model.answerLayout.textBoundingSize.height;
        self.answerLabel.textLayout = self.model.answerLayout;
        
        self.contentSize = CGSizeMake(SCREEN_WIDTH, self.answerBtn.bottom + 32.0);
    }
}

- (void)bindModel:(PaperTestModel *)model {
    _model = model;
    
    [self refreshView];
}

- (void)didAnswerAction:(UIButton *)btn {
    if (self.model.isShow == NO) {
        self.model.isShow = YES;
        [self showAnswerViewWithType:self.model.isShow];
    }
}

#pragma mark - getter

- (YYLabel *)textLabel {
    if (_textLabel == nil) {
        _textLabel = [YYLabel new];
        _textLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
        _textLabel.displaysAsynchronously = YES;
        _textLabel.ignoreCommonProperties = YES;
        _textLabel.fadeOnAsynchronouslyDisplay = NO;
        _textLabel.fadeOnHighlight = NO;
    }
    return _textLabel;
}

- (YYLabel *)answerLabel {
    if (_answerLabel == nil) {
        _answerLabel = [YYLabel new];
        _answerLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
        _answerLabel.displaysAsynchronously = YES;
        _answerLabel.ignoreCommonProperties = YES;
        _answerLabel.fadeOnAsynchronouslyDisplay = NO;
        _answerLabel.fadeOnHighlight = NO;
    }
    return _answerLabel;
}

- (UIButton *)answerBtn {
    if (_answerBtn == nil) {
        _answerBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 42.0f)];
        _answerBtn.backgroundColor = MainThemeColor;
        [_answerBtn setTitle:@"查看参考答案" forState:UIControlStateNormal];
        [_answerBtn addTarget:self action:@selector(didAnswerAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _answerBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
